home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1313_disabling_and_enabling_menuCell.rtf < prev    next >
Text File  |  1995-06-12  |  4KB  |  115 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f2\fswiss Helvetica;\f3\fmodern Ohlfs;}
  2. \paperw12140
  3. \paperh8500
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc0\cf0 Q:  How do I disable/enable menu cells?\
  8. \
  9. A:  The method 
  10. \f1\fs24  setUpdateAction:(SEL)aSelector forMenu:aMenu 
  11. \f0\fs28 is responsible for the menu cell update,  where 
  12. \f1\fs24 aSelector
  13. \f0\fs28  is your own update method, and 
  14. \f1\fs24 aMenu
  15. \f0\fs28  the menu you want to update. This method has to be called inside 
  16. \f1\fs24 appDidInit:
  17. \f0\fs28  because NXApp is responsible for the update.  Doing so makes your program more efficient, since it needs to be done only once.\
  18. \
  19. Q: Should I use the application method 
  20. \f1\fs24 setAutoupdate:(BOOL)flag 
  21. \f0\fs28  or the menu 
  22. \f1\fs24 update
  23. \f0\fs28  method for updating the menu items? What are the trade-offs?\
  24. \
  25.  
  26. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc1\cf1 A: The application method 
  27. \f1\fs24 setAutoupdate:YES
  28. \f0\fs28  causes the menus to stay up to date all the time (i.e., it only updates them when they are on-screen).  Calling 
  29. \f1\fs24 update
  30. \f0\fs28  directly every time something changes might be a lot of extra work for nothing. However,  if things rarely change in the menu, it might be worth not incurring the window list traversal 
  31. \f1\fs24 setAutoupdate:
  32. \f0\fs28  causes.
  33. \f2\fs24 \
  34.  
  35. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 \
  36. \
  37. Example (adapted from /NextDeveloper/Examples/Draw)\
  38. \
  39.  
  40. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f1\fs24\fc1\cf1 static void initMenu(id menu)\
  41. /* A private C function:\
  42.  * Sets the updateAction for every menu item which sends to the\
  43.  * First Responder (i.e. their target is nil).  \
  44.  * Returns the active menu if is found in this menu.\
  45.  */ \
  46. \{\
  47.     int count;\
  48.     id matrix, cell;\
  49.     id matrixTarget, cellTarget;\
  50. \
  51.     matrix = [menu itemList];\
  52.     matrixTarget = [matrix target];\
  53. \
  54.     count = [matrix cellCount];\
  55.     while (count--) \{\
  56.     cell = [matrix cellAt:count :0];\
  57.     cellTarget = [cell target];\
  58.     if (!matrixTarget && !cellTarget) \{\
  59.         [cell setUpdateAction:@selector(menuItemUpdate:) forMenu:menu];\
  60.     \} else if ([cell hasSubmenu]) \{\
  61.         initMenu(cellTarget);\
  62.     \}\
  63.     \}\
  64. \}\
  65. \
  66.  
  67. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 \
  68.  
  69. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f1\fs24\fc1\cf1 /* Initializes the updateAction method for the menu cells.\
  70.    setAutoupdate:YES means that updateWindows will be called\
  71.    after every event is processed (to keep the menu items up to date).\
  72.  */\
  73. - appDidInit:sender\
  74. \{\
  75.    ........\
  76.    initMenu([NXApp mainMenu]);\
  77.    [NXApp setAutoupdate:YES];\
  78.    return self;\
  79. \}\
  80. \
  81. #define        TOGGLE    4         /* Some integer cell tag value    */\
  82. \
  83. - (BOOL)menuItemUpdate:menuCell\
  84. /* Here goes your own code for menu cell update. This is only a simple example \
  85.    which toggles the state of the selected cell. The return value YES means\
  86.    that the menu will be updated.\
  87. */\
  88. \{\
  89.    \
  90.     BOOL        cellState;\
  91. \
  92.     \
  93.     switch ([menuCell tag])\
  94.     \{\
  95.         case TOGGLE: /* toggle menu cell state    */\
  96.             cellState = [menuCell isEnabled];\
  97.             [menuCell setEnabled:!cellState];            \
  98.             return YES;\
  99.         default:\
  100.             break;\
  101.     \}\
  102. \
  103.     return NO;\
  104. \}\
  105.  
  106. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 \
  107. QA636\
  108.  
  109. \fc1\cf1 \
  110.  
  111. \pard\tx520\tx1060\tx1600\tx2120\tx2660\tx3200\tx3720\tx4260\tx4800\tx5320\fc1\cf1 Valid for 2.0, 3.0
  112. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\fc1\cf1 \
  113. \
  114.  
  115.